package middlewares

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"wskyygyso/api/app/models/user"
	"wskyygyso/api/pkg/config"
	"wskyygyso/api/pkg/jwt"
	"wskyygyso/api/pkg/response"
)

func AuthJwt() gin.HandlerFunc {
	return func(context *gin.Context) {
		claims, err := jwt.NewJwt().ParserToken(context)
		if err != nil {
			response.Unauthorized(context, fmt.Sprintf("请查看 %v 相关的接口认证文档", config.Get("app.name")))
			return
		}
		userModel := user.Get(claims.UserId)
		if userModel.ID == 0 {
			response.Unauthorized(context, "未找到用户或用户已被删除")
			return
		}
		context.Set("current_user_id", userModel.GetStringId())
		context.Set("current_user_name", userModel.Name)
		context.Set("current_user", userModel)
		context.Next()
	}
}
